home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
fincalc.exe
/
FINCALC.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-09
|
11KB
|
347 lines
//Eddy Vasile, PO Box 71313, LA CA 90071 or 70451,3333@COMPUSERVE
//A simple program to illustrate financial concepts, classes
//and data entry and display using C++
//
#include <math.h> //need this for power, log and exp
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
//financial events are classes
class Financial {
double PV; //present value
double FV; //future value these guys are accessed only inside
double IntRate; //interest rate
double NPer; //number of periods
public:
//construct class by initializing with parameter values
//the member functions calculate and return their own values
//if value is -1 then it needs to be calcualted, else it's given
Financial(double InPV, double InFV, double InIntRate, double InNPer) {
PV=InPV; FV=InFV; IntRate=InIntRate; NPer=InNPer;}
double GetPV() {if (PV==-1) PV=FV/pow(1+IntRate,NPer); return PV;}
double GetFV() {if (FV==-1) FV=PV*pow(1+IntRate,NPer); return FV;}
double GetNPer() {if (NPer==-1) NPer=(log(FV)-log(PV))/log(1+IntRate);return NPer;}
double GetIntRate() {if (IntRate==-1) IntRate=exp((log(FV)-log(PV))/NPer)-1;return IntRate;}
};
//function to open a window and a box around it
//parameters are dimensions, border, fore and background colors
void BoxWin(int x1,int y1,int x2,int y2,int BD,int FG,int BG)
{
int i,TB,SID,TLC,TRC,BLC,BRC;
window(1,1,80,25);
textcolor(FG) ;
textbackground(BG);
switch (BD) {
case 0:; //No border option}
case 1:
TB = 196; //Top Border
SID = 179; //Side Border
TLC = 218; //Top Left Corner
TRC = 191; //Top Right Corner
BLC = 192; //Bottom Left Corner
BRC = 217; //Bottom Right Corner
break;
case 2:
TB = 205;
SID = 186;
TLC = 201; TRC = 187;
BLC = 200; BRC = 188;
break;
default:
BD = 3;
TB = 205;
SID = 179;
TLC = 213; TRC = 184;
BLC = 212; BRC = 190;
break;
}
if(BD > 0) { // border?
// Top
gotoxy(x1,y1); // Window Origin
putch(TLC); // Top Left Corner
for (i=x1+1; i<x2; i++) // Top Bar
putch(TB);
putch(TRC); // Top Right Corner
// Sides
for (i=y1+1; i<y2; i++) {
gotoxy(x1,i); // Left Side Bar
putch(SID);
gotoxy(x2,i) ; // Right Side Bar
putch(SID);
}
// Bottom
gotoxy(x1,y2); // Bottom Left Corner
putch(BLC);
for (i=x1+1; i<x2; i++) // Bottom Bar
putch(TB);
putch(BRC); // Bottom Right Corner
}
window(x1+1,y1+1,x2-1,y2-1);
clrscr();
textcolor(FG) ;
textbackground(BG);
}
//sound a deep throated burp-beep
void beep()
{
sound(50);
delay(200);
nosound();
}
//create a string made out of same chars
char *repeatstr(int c, int l)
{
char s[80];
int i;
if (l>=80) l=79;
for (i=0;i<l;i++) s[i]=c;
s[l]='\0';
return(s);
}
//create, edit and grab a string from somewhere on the screen
//**warning, use absolute window locations 'cause gettext doesn't respect windows
int inputstr(char s[80],char *goodchars,int l, int x, int y)
{
int i,j,p=0,tc=0,sl=strlen(s),Blob=177;
char ch;
char buffer[4096];
if (l>=80) l=79;
gotoxy(x,y);
printf("%s%s",s,repeatstr(Blob,l-sl));
do {
gotoxy(x+p,y);
ch=getch();
if (ch==0) {
ch=getch();
switch (ch) {
case 15 : tc=-1;break; //back tab
case 'I' : tc=-1;break; //page up
case 'Q' : tc= 1;break; //page down
case 'H' : tc=-1;break; //up
case 'P' : tc= 1;break; //down
case 'K' : if (p>0) { //left
p--;
gotoxy(x+p,y);
}
else beep();
break;
case 'M' : if (p<sl) { //right
p++;
gotoxy(x+p,y);
}
else beep();
break;
case 'G' : if (sl*p!=0) { //home
p = 0;
gotoxy(x,y);
}
else beep();
break;
case 'O' : if (sl>0 && p<sl) {//end
p=sl-1;
gotoxy(x+p,y);
}
else beep();
break;
case 'R' : beep();break; //insert - screw it, no insert, too much work
case 'S' : if (p<sl) { //delete
sl--;
for (i=p+1;i<=sl;i++) putch(s[i]);
putch(Blob);
gotoxy(x+p,y);
}
break;
default : beep();
}
}
else {
if (ch>=' ' && ch<='~') {
if (strchr(goodchars,ch) && p<l) {
putch(ch);
p++;
sl++;
gotoxy(x+p,y);
}
else beep();
}
if (ch==8) { //back space
if (p*sl==0) beep();
else {
sl--;
p--;
gotoxy(x+p,y);
for (i=p+1;i<=sl;i++) putch(s[i]);
putch(Blob);
}
}
if (ch==9 || ch==13) tc=1; //tab or return
if (ch==27) tc=2; //exit input completely
}
gettext(x,y,x+l,y,buffer);
i=0;j=0;
strcpy(s,"\0");
while (i<=l && buffer[j]!=Blob) {
if (strchr(goodchars,buffer[j])) s[i++]=buffer[j];
j +=2;
}
s[i]='\0';
} while (tc==0); //tc=1 means go to next item, -1 prior, 2= done
return tc;
}
//function to display all members formatted
void PrintAll(Financial F) {
BoxWin(20,15,65,22,2,WHITE,GREEN);
gotoxy(1,1);cputs("Present Value: ");
gotoxy(1,2);cputs("Future Value : ");
gotoxy(1,3);cputs("Periods : ");
gotoxy(1,4);cputs("Interest Rate: ");
gotoxy(17,1);printf("%11.2f",F.GetPV());
gotoxy(17,2);printf("%11.2f",F.GetFV());
gotoxy(17,3);printf("%11.2f",F.GetNPer());
gotoxy(17,4);printf("%11.2f",F.GetIntRate()*100);
gotoxy(1,5);cputs("Press any key.."); getch();
}
//give some help
void HelpMsg()
{
window(1,1,80,25);
clrscr();
BoxWin(1,8,80,20,1,RED,CYAN);
cputs("This simple C++ program illustrates object oriented techniques for finance. \r\n");
cputs("Financial events are CLASSES with 4 members: Present Value, Future Value, \r\n");
cputs("Interest Rate and Number of Periods. The Present Value PV is the $ amount \r\n");
cputs("deposited at Interest Rate R in order to obtain the Future Value FV after the\r\n");
cputs("Number of Periods N. In order to calculate any member, the other 3 must be \r\n");
cputs("known in order to satisfy the formula FV-PV(1+R)^N=0. Choose the calculation \r\n");
cputs("you want and enter the requested values. Example: choose the Future Value \r\n");
cputs("Calculation. You will be asked to enter PV R and N. Enter 5000 6.75 3 to \r\n");
cputs("calculate the Future Value of a $5,000 deposit @6.75% for 3 years. The result\r\n");
cputs("is $6,082.38. Press any key to continue.. \r\n");getch();
BoxWin(1,1,80,11,2,YELLOW,BLUE);
cputs("To compound interest monthly, enter N as 36 (3 X 12) and R as 0.563 \r\n");
cputs("(monthly interest = 6.75/12). FV is now $6,119.92. This is useful if you \r\n");
cputs("want to calculate the YIELD of a CD. Now that you know the monthly compounded\r\n");
cputs("FV, choose the Interest Calculation and enter 5000 6119.92 3. The YIELD is \r\n");
cputs("6.97%. Modify the source code to compound weekly or daily. If you like this, \r\n");
cputs("write a note to Eddy Vasile, PO Box 71313, LA, CA 90071 or 70451,3333 at \r\n");
cputs("CompuServe and I'll send you the code for Net Present Value and Internal Rate\r\n");
cputs("of Return calculations with more elaborate functions. Press any key.. \r\n");
getch();
window(1,1,80,25);
clrscr();
}
//put prompts on the screen and stay in loop grabbing data 'till escape
void getdata(char string1[], char string2[], char string3[],
double *d1, double *d2, double *d3)
{
char s1[80],s2[80],s3[80];
char *digits="0123456789.";
int rc=0,where=0;
strcpy(s1,"\0");strcpy(s2,"\0");strcpy(s3,"\0"); *d1= *d2= *d3= 0;
window(1,1,80,25);
clrscr();
BoxWin(1,1,70,5,2,YELLOW,BLUE); //just for looks
cputs("Complete EVERY field, use Arrows, Tabs, Pg